package com.example.bite.aaa;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;

import jakarta.servlet.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RequestMapping("/User")
@RestController
@CrossOrigin
public class UserController {
/*    @RequestMapping("/show")
    public List<Student> show() {
        Student student1 = new Student();
        student1.setName("zhangsan");
        student1.setAge(12);
        student1.setGender("男");
        Student student2 = new Student();
        student2.setName("lisi");
        student2.setAge(12);
        student2.setGender("男");
        List<Student> list = new ArrayList<>();
        list.add(student1);
        list.add(student2);


        System.out.println(list);
        return list;
    }*/


/*    @RequestMapping("/multi")
    public String multi(String name,int age)
    {
        return "接收到的name："+name+"接收到的age:"+age;
    }
    @RequestMapping("/name")
    public String name(String name)
    {
        return "接受到的参数"+name;
    }*/

/*    @RequestMapping("/request")
    public String request(int age)
    {
        return "接受到的age:"+age;
    }

    @RequestMapping("/sayHi")
    public String sayHi()
    {
        return "hello SpringMVC";
    }*/

    @RequestMapping("/param1")
    public String Param1(String username)
    {
        System.out.println("收到！");
        return "接受到的param1："+username;
    }

    //@RequestParam("前端参数名称" 参数类型 后端参数名) 这个注解是将前端和后端的参数绑定
    @RequestMapping("/param2")
    public String Param2(@RequestParam("name") String username)
    {
        System.out.println("后端Param2收到:"+username);
        return "接受到的param2："+username;
    }

    //在进行传参的时候，传的参数类型一定是包装类，而不是基本类型
    @RequestMapping("/param3")
    public String Param3(Integer age)
    {
        System.out.println("后端Param3收到:"+age);
        return "接受到的param3："+age;
    }


    @RequestMapping("/param4")
    public String Param4(int age)
    {
        return "接受到的param4："+age;
    }


    //上传文件
    @RequestMapping("/getFile")
    public String getFile(@RequestPart("file")MultipartFile file) throws IOException {
        //获取文件名
        String filename = file.getOriginalFilename();
        //上传文件到指定的路径
        file.transferTo(new File("D:/JS/"+file.getOriginalFilename()));
        return "接受到文件的名称为："+filename;
    }

    //获取Cookie
    @RequestMapping("/getCookie")
    public String getCookie(HttpServletRequest request, HttpServletResponse response)
    {
        Cookie[] cookies = request.getCookies();
        StringBuilder sb = new StringBuilder();
        if(cookies != null)
        {
            for(Cookie cookie : cookies)
            {
                sb.append(cookie.getName()+":"+cookie.getValue());
                System.out.println(cookie.getName()+":"+cookie.getValue());
            }
        }
        return "接受到Cookie!";
    }

    //获取Ssesion
    @RequestMapping("/setSession")
    public String setSsesion(HttpServletRequest request)
    {
        HttpSession session = request.getSession();
        if(request!=null)
        {
            session.setAttribute("username","java");
        }
        return "session存储成功！";
    }

    /**
     * 获取session*/
    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request)
    {
        HttpSession session = request.getSession(false);
        String username = null;
        if(session!=null && session.getAttribute(username)!=null){
            username = (String)session.getAttribute("username");
        }
        return "username:"+username;
    }
    /** 返回Json
     * */
    @RequestMapping("/returnJson")
    public UserInfo returnJson(){
        UserInfo userInfo = new UserInfo("zhangsan","123@qq.com","18","ADMIN");
        return userInfo;
    }

    /**
     * */
    @RequestMapping(value = "/returnJson1")
    public Object returnJson1(@RequestBody UserInfo userInfo)
    {
        return userInfo.toString();
    }
}
